package com.hcy.performancepro.service.performanceTool.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fuled.test.tool_config.DisableUtil;
import com.hcy.performancepro.bo.performanceTool.RecordEntityBo;
import com.hcy.performancepro.dao.performanceTool.PerformanceToolControllerDao;
import com.hcy.performancepro.dao.performanceTool.PerformanceToolEntityDao;
import com.hcy.performancepro.dao.performanceTool.PerformanceToolServiceDao;
import com.hcy.performancepro.po.MyResultData;
import com.hcy.performancepro.po.performanceTool.RecordController;
import com.hcy.performancepro.po.performanceTool.RecordEntity;
import com.hcy.performancepro.po.performanceTool.RecordService;
import com.hcy.performancepro.service.performanceTool.PerformanceToolEntityService;
import com.hcy.performancepro.vo.performanceTool.RecordControllerVo;
import com.hcy.performancepro.vo.performanceTool.RecordEntityVo;
import com.hcy.performancepro.vo.performanceTool.RecordServiceVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("performanceToolEntityServiceImpl")
@DisableUtil
public class PerformanceToolEntityServiceImpl extends ServiceImpl<PerformanceToolEntityDao, RecordEntity> implements PerformanceToolEntityService {

    public Wrapper<RecordEntity> getWrapper(RecordEntityBo recordEntityBo){
        LambdaQueryWrapper<RecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getRequestId()), RecordEntity::getRequestId, recordEntityBo.getRequestId());
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getTimestamp()), RecordEntity::getTimestamp, recordEntityBo.getTimestamp());
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getUser()), RecordEntity::getUser, recordEntityBo.getUser());
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getIpAddress()), RecordEntity::getIpAddress, recordEntityBo.getIpAddress());
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getTotalCostTime()), RecordEntity::getTotalCostTime, recordEntityBo.getTotalCostTime());
        wrapper.like(StrUtil.isNotBlank(recordEntityBo.getCreateTime()), RecordEntity::getCreateTime, recordEntityBo.getCreateTime());
        /*HashMap<String, String> map = recordEntityBo.getOrderInfo();
        if(map!=null&&!map.isEmpty()){
            map.forEach((info,order)->{
                switch (info) {
                    case "requestId":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getRequestId);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getRequestId);
                        }
                        break;
                    case "timestamp":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getTimestamp);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getTimestamp);
                        }
                        break;
                    case "user":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getUser);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getUser);
                        }
                        break;
                    case "ipAddress":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getIpAddress);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getIpAddress);
                        }
                        break;
                    case "totalCostTime":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getTotalCostTime);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getTotalCostTime);
                        }
                        break;
                    case "createTime":
                        if ("desc".equalsIgnoreCase(order)) {
                            wrapper.orderByDesc(RecordEntity::getCreateTime);
                        } else {
                            wrapper.orderByAsc(RecordEntity::getCreateTime);
                        }
                        break;
                    default:
                        break;
                }
            });
        }*/
        return wrapper;
    }

    @Override
    public List<RecordEntity> sarchlist(Integer currentPage, Integer pageSize, RecordEntityBo recordEntityBo) {
        return baseMapper.selectList(new Page<>(currentPage, pageSize), getWrapper(recordEntityBo));
    }

    @Override
    public Long countNumber(RecordEntityBo recordEntityBo) {
        return baseMapper.selectCount(getWrapper(recordEntityBo));
    }
}
